package com.suyx.job.controller;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.suyx.job.config.RedisUtils;
import com.suyx.job.entity.DeviceInfo;
import com.suyx.job.entity.Page;
import com.suyx.job.entity.Result;
import com.suyx.job.service.IDeviceService;

import lombok.extern.slf4j.Slf4j;

/**
 * @Auther: lanyanhua
 * @Date: 2018/10/31 0031 10:10
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/device")
public class DeviceController  {

    private IDeviceService deviceService;

    @Autowired
    public DeviceController(IDeviceService deviceService) {
        this.deviceService = deviceService;
    }
    /**
     * 查询设备信息
     */
    @GetMapping(value="/findDevicePage")
    public Page findDevicePage(DeviceInfo deviceInfo, Integer page, Integer limit)
    {
        return deviceService.findDeviceInfoPage(deviceInfo,page-1,limit);
    }

    /**
     * 保存设备信息
     */
    @PostMapping("/saveDevice")
    public Result saveDevice(DeviceInfo deviceInfo){
        try {
        	deviceService.saveAndFlush(deviceInfo);
            return new Result(true,"保存成功!");
        }catch (Exception e){
            log.error("保存设备信息数据报错："+ExceptionUtils.getStackTrace(e));
            return new Result(false,"保存失败："+e.getMessage());
        }
    }

    /**
     * 删除设备信息
     */
    @PostMapping("deleteDevice")
    public Result deleteDevice(Integer deviceId){
        try {
        	deviceService.deleteById(deviceId);
            return new Result(true,"删除成功!");
        }catch (Exception e){
            log.error("删除设备信息数据报错：" + ExceptionUtils.getStackTrace(e));
            return new Result(false,"删除失败："+e.getMessage());
        }
    }



}
